package com.loiane.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONObject;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.loiane.dataaccess.CheckBatchLogServerDatabaseAccessor;
import com.loiane.dataaccess.CheckDpropServerDatabaseAccessor;
import com.loiane.model.BatchLogServerItem;
import com.loiane.model.DproperServerItem;

@Controller
public class BatchLogServerConfigController {
	@RequestMapping("/batchlogcreate.do")
	public @ResponseBody String dpropserverCreate(){
		System.err.println("invoke dprop server create......");
		Map<String,Object> map = new HashMap<String,Object>();
		
		map.put("success", true);
		JSONObject jso = JSONObject.fromObject(map);
		return jso.toString();		
	}
	
	@RequestMapping("/batchlogupdate.do")	
	public @ResponseBody String batchlogUpdate(@RequestBody String names){
		System.err.println("invoke dprop server update......");
		Map<String,Object> map = new HashMap<String,Object>();
		
		map.put("success", true);
		JSONObject jso = JSONObject.fromObject(map);
//		Object obj = JSONObject.fromObject(names).get("names");
//		TransformerItem ti = null;
		
//		if(obj instanceof JSONArray){
//			@SuppressWarnings("unchecked")
//			Iterator<JSONObject> it = ((JSONArray) obj).iterator();
//			while(it.hasNext()){
//				JSONObject js = it.next();
//				ti = new TransformerItem();
//				ti.setId(Integer.valueOf(js.get("id").toString()));
//				ti.setName(js.get("name").toString());
//			}
//		}else if(obj instanceof JSONObject){
//			
//		}
		return names;
	}
	
	@RequestMapping("/batchlogdestroy.do")
	public @ResponseBody String batchlogDestroy(){
		System.err.println("invoke dprop server destroy......");
		Map<String,Object> map = new HashMap<String,Object>();
		
		map.put("success", true);
		JSONObject jso = JSONObject.fromObject(map);
		return jso.toString();		
	}
	
	@RequestMapping("/batchlogread.do")
	public @ResponseBody String batchlogRead(){
		System.err.println("invoke batch log read......");
		Map<String,Object> map = new HashMap<String,Object>();
		CheckBatchLogServerDatabaseAccessor cba = new CheckBatchLogServerDatabaseAccessor();
		List<BatchLogServerItem> batchlogserver = cba.listAllBatchLogItems();
		map.put("success", true);
		map.put("servers", batchlogserver);
		JSONObject jso = JSONObject.fromObject(map);
		return jso.toString();		
	}
}
